<?php

class TagsDependency extends CComponent implements ICacheDependency
{

    protected $timestamp;
    private $_tags;

    /**
     * В качестве параметров передается список тегов
     *
     * @params tag1, tag2, ..., tagN
     */
    public function __construct()
    {
	$tags = func_get_args();
	if (!empty($tags)) {
	    $this->setTags($tags);
	}
    }

    public function setTags($tags)
    {
	$this->_tags = $tags;
    }

    public function getTags()
    {
	if ($this->_tags === null) {
	    $this->_tags = array();
	}
	return $this->_tags;
    }

    /**
     * Evaluates the dependency by generating and saving the data related with dependency.
     * This method is invoked by cache before writing data into it.
     */
    public function evaluateDependency()
    {
	$this->timestamp = time();
    }

    /**
     * @return boolean whether the dependency has changed.
     */
    public function getHasChanged()
    {
	$tags = array();	
	foreach ($this->tags as $tag) {
	    $tags[] = TaggingBehavior::PREFIX . $tag;
	}
	$values = CacheManager::getCache('memcache')->mget($tags);
	foreach ($values as $value) {
	    if ((integer) $value > $this->timestamp) {
		return true;
	    }
	}

	return false;
    }

}

?>
